package com.googlecode.mylyn.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.CheckForNull;

import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;

/**
 * Utility class that helps converting Google priorities into Mylyn priorities
 * and back. In addition it provides predefined labels for type and status attributes.
 * 
 *  <p>This class is only a temporary solution untils we can access the project
 *  metadata (Issue 7).</p>
 */
final class LabelUtils {

    static final String PRIORITY_LOW = "Low";
    static final String PRIORITY_MEDIUM = "Medium";
    static final String PRIORITY_HIGH = "High";
    static final String PRIORITY_CRITICAL = "Critical";

    private LabelUtils() {
        throw new AssertionError("not instantiable");
    }
    
    /**
     * Converts a Google code priority String to a Mylyn priority level.
     * 
     * @param priority the Google code priority
     * @return the Mylyn priority level, may be {@literal null}
     */
    @CheckForNull
    static PriorityLevel convertToMylyn(String priority) {
        if (PRIORITY_CRITICAL.equals(priority)) {
            return PriorityLevel.P1;
        } else if (PRIORITY_HIGH.equals(priority)) {
            return PriorityLevel.P2;
        } else if (PRIORITY_MEDIUM.equals(priority)) {
            return PriorityLevel.P3;
        } else if (PRIORITY_LOW.equals(priority)) {
            return PriorityLevel.P4;
        }
        return null;
    }
    
    /**
     * Converts a Mylyn priority level to a Google code priority String.
     * 
     * @param level the Mylyn priority level
     * @return the the Google code priority String, may be {@literal null}
     */
    @CheckForNull
    static String convertToGoogle(PriorityLevel level) {
        if (level == null) {
            return null;
        }
        
        switch (level) {
        case P1:
            return PRIORITY_CRITICAL;
        case P2:
            return PRIORITY_HIGH;
        case P3:
            return PRIORITY_MEDIUM;
        case P4:
            return PRIORITY_LOW;
        }
        return null;
    }


    public static void fillPriorityOptions(TaskAttribute priority) {
        priority.putOption(PriorityLevel.P1.toString(), LabelUtils.PRIORITY_CRITICAL);
        priority.putOption(PriorityLevel.P2.toString(), LabelUtils.PRIORITY_HIGH);
        priority.putOption(PriorityLevel.P3.toString(), LabelUtils.PRIORITY_MEDIUM);
        priority.putOption(PriorityLevel.P4.toString(), LabelUtils.PRIORITY_LOW);
    }

    public static void fillStatusOptions(TaskAttribute statusAttribute) {
        List<String> statusOptions = new ArrayList<String>();
        statusOptions.add("New");
        statusOptions.add("Accepted");
        statusOptions.add("Started");
        statusOptions.add("Fixed");
        statusOptions.add("Verified");
        statusOptions.add("Invalid");
        statusOptions.add("Duplicate");
        statusOptions.add("WontFix");
        statusOptions.add("Done");
        
        fillAttribute(statusAttribute, statusOptions);
    }

    private static void fillAttribute(TaskAttribute statusAttribute, List<String> statusOptions) {
        for (Iterator<String> iterator = statusOptions.iterator(); iterator.hasNext();) {
            String option = iterator.next();
            statusAttribute.putOption(option, option);
        }
    }

    public static void fillTypeOptions(TaskAttribute typeAttribute) {
        List<String> typeOptions = new ArrayList<String>();
        typeOptions.add("Defect");
        typeOptions.add("Enhancement");
        typeOptions.add("Task");
        typeOptions.add("Review");
        typeOptions.add("Other");
        
        fillAttribute(typeAttribute, typeOptions);
    }
}
